package xyz.schwi.cels.dao.impl;

import xyz.schwi.cels.dao.BaseDao;
import xyz.schwi.cels.entity.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author schwi
 * @date 2021/9/29 0029  10:17
 */
public class BaseDaoImpl implements BaseDao {
    @Override
    public List<Word> selectWords(String path) throws IOException {
        List<Word> words = new ArrayList<>();
        List<String> lines = Files.readAllLines(Path.of(path));
        lines.forEach(s -> {
            String[] s1 = s.split(" ");
            String[] s2 = s1[1].split(":");
            List<String> list = Arrays.stream(s2).collect(Collectors.toList());
            Word word = new Word(s1[0], list);
            words.add(word);
        });
        for (int i = 0; i < words.size(); i++) {
            words.get(i).setId((long) (i + 1));
        }
        return words;
    }

    @Override
    public List<Vocabulary> selectVocabularies(String path) throws IOException {
        List<Vocabulary> vocabularies = new ArrayList<>();
        List<String> lines = Files.readAllLines(Path.of(path));
        lines.forEach(s -> {
            String[] s1 = s.split("#");
            String[] s2 = s1[1].split(":");
            List<String> list = Arrays.stream(s2).collect(Collectors.toList());
            Vocabulary vocabulary = new Vocabulary(s1[0], list, s1.length == 3 ? s1[2] : null);
            vocabularies.add(vocabulary);
        });
        for (int i = 0; i < vocabularies.size(); i++) {
            vocabularies.get(i).setId((long) (i + 1));
        }
        return vocabularies;
    }

    @Override
    public Map<Character, List<Word>> getWordsMap(String path) throws IOException {
        return selectWords(path).stream().collect(Collectors.groupingBy(Word::getFirstLitter));
    }

    @Override
    public List<Base> selectHistory(String path) throws IOException {
        List<Base> history = new ArrayList<>();
        List<String> lines = Files.readAllLines(Path.of(path));
        lines.forEach(s -> {
            if (s.startsWith("#")) {
                String[] s1 = s.split("#");
                String[] s2 = s1[2].split(":");
                List<String> list = Arrays.stream(s2).collect(Collectors.toList());
                Vocabulary vocabulary = new Vocabulary(s1[1], list, "".equals(s1[3]) ? null : s1[3]);
                vocabulary.setId(Long.getLong(s1[4]));
                history.add(vocabulary);
            } else {
                String[] s1 = s.split(" ");
                String[] s2 = s1[1].split(":");
                List<String> list = Arrays.stream(s2).collect(Collectors.toList());
                Word word = new Word(s1[0], list);
                word.setId(Long.getLong(s1[2]));
                history.add(word);
            }
        });
        return history;
    }

    @Override
    public List<Base> selectError(String path) throws IOException {
        List<Base> error = new ArrayList<>();
        List<String> lines = Files.readAllLines(Path.of(path));
        lines.forEach(s -> {
            if (s.startsWith("#")) {
                String[] s1 = s.split("#");
                String[] s2 = s1[2].split(":");
                List<String> list = Arrays.stream(s2).collect(Collectors.toList());
                ErrorVocabulary vocabulary = new ErrorVocabulary(s1[1], list, "".equals(s1[3]) ? null : s1[3], "".equals(s1[3]) ? null : Integer.decode(s1[5]));
                vocabulary.setId(Long.getLong(s1[4]));
                error.add(vocabulary);
            } else {
                String[] s1 = s.split(" ");
                String[] s2 = s1[1].split(":");
                List<String> list = Arrays.stream(s2).collect(Collectors.toList());
                ErrorWord word = new ErrorWord(s1[0], list, Integer.decode(s1[3]));
                word.setId(Long.getLong(s1[2]));
                error.add(word);
            }
        });
        return error;
    }
}
